﻿using Microsoft.Xna.Framework;
using System;

namespace JRBase
{
    public abstract class Collideable

    {
        protected Rectangle rectangle;
        protected bool onBottom;

        public bool OnBottom
        {
            get { return onBottom; }
            set { onBottom = value; }
        }

        protected bool onTop;

        public bool OnTop
        {
            get { return onTop; }
            set { onTop = value; }
        }

        public int X
        {
            get { return rectangle.X; }
            set { rectangle.X = value; }
        }

        public int Y
        {
            get { return rectangle.Y; }
            set { rectangle.Y = value; }
        }

        public Rectangle Rectangle
        {
            get { return rectangle; }
        }

        protected void Initialize(Rectangle rectangle)
        {
            this.rectangle = rectangle;
        }


        public bool collidesWith(Collideable element)
        {
            return rectangle.Intersects(element.Rectangle);
        }

        public void keepOutOf(Collideable element)
        {
            float top = Math.Abs((this.rectangle.Y + this.rectangle.Height) - element.Rectangle.Y);
            float bottom = Math.Abs(this.rectangle.Y - (element.Rectangle.Y + element.Rectangle.Height));
            float left = Math.Abs((this.rectangle.X + this.rectangle.Width) - element.Rectangle.X);
            float right = Math.Abs(this.rectangle.X - (element.Rectangle.X + element.Rectangle.Width));

            if (top <= bottom && right <= left)
            {
                if (top < right)
                {
                    onTop = false;
                    onBottom = true;
                    rectangle.Y = element.Rectangle.Y - rectangle.Height;
                }
                if (top >= right)
                {
                    onTop = false;
                    onBottom = false;
                    rectangle.X = element.Rectangle.X + element.Rectangle.Width;
                }
            }

            if (top > bottom && right <= left)
            {
                if (bottom < right)
                {
                    onTop = true;
                    onBottom = false;
                    rectangle.Y = element.Rectangle.Y + element.Rectangle.Height;
                }
                if (bottom >= right)
                {
                    onTop = false;
                    onBottom = false;
                    rectangle.X = element.Rectangle.X + element.Rectangle.Width;
                }
            }

            if (top <= bottom && right > left)
            {
                if (top < left)
                {
                    onTop = false;
                    onBottom = true;
                    rectangle.Y = element.Rectangle.Y - rectangle.Height;
                }
                if (top >= left)
                {
                    onTop = false;
                    onBottom = false;
                    rectangle.X = element.Rectangle.X - rectangle.Width;
                }
            }

            if (top > bottom && right > left)
            {
                if (bottom < left)
                {
                    onTop = true;
                    onBottom = false;
                    rectangle.Y = element.Rectangle.Y + element.Rectangle.Width;
                }
                if (bottom >= left)
                {
                    onTop = false;
                    onBottom = false;
                    rectangle.X = element.Rectangle.X - rectangle.Width;
                }
            }
        }
    }
}
